package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.CommonConstants;
import com.zlm.common.constants.NumberConstants;
import com.zlm.common.dto.CoordinatesDto;
import com.zlm.common.dto.userEmp.PartnerResponseDto;
import com.zlm.common.dto.baidurequest.BaiduResultDto;
import com.zlm.common.entity.SysPartnerDO;
import com.zlm.common.entity.UserTokenDO;
import com.zlm.common.enums.UserTypeEnum;
import com.zlm.common.util.BaiduMapUtils;
import com.zlm.user.emp.dao.SysPartnerDao;
import com.zlm.user.emp.dao.SysUnderlingDao;
import com.zlm.user.emp.dao.UserTokenDao;
import com.zlm.user.emp.service.SysPartnerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: duyufu
 * @Date: 2019/5/5 16:01
 */
@Service
public class SysPartnerServiceImpl extends AbstractBaseService<SysPartnerDao, SysPartnerDO> implements SysPartnerService {
    @Autowired
    @Override
    public void setBaseDao(SysPartnerDao sysPartnerDao) {
        this.baseDao = sysPartnerDao;
    }
    @Autowired
    private SysUnderlingDao sysUnderlingDao;
    @Autowired
    private UserTokenDao userTokenDao;

    @Override
    public PageEntity queryLocalPartner(Integer pageNumber, Integer pageSize, CoordinatesDto coordinates,
                                        String token) {
        String city = getCity(coordinates);
        int count = baseDao.countPartner(city);
        PageEntity pageEntity = setPage(pageNumber, pageSize);
        List<PartnerResponseDto> partnerList = baseDao.queryPartnerList(pageEntity.getPageNumber(),
                pageEntity.getPageSize(),city);
        List<Integer> myPartnerList = null;
        if (null != token) {
            UserTokenDO loginUser = userTokenDao.getLoginUser(token);
            if (null != loginUser) {
                Integer empId = loginUser.getUserId();
                myPartnerList = sysUnderlingDao.queryUnderlingList(empId);
            }else {
                throw new RuntimeException(CodeConstants.CODE_FAIL);
            }
        }
        buildPartner(partnerList,myPartnerList);
        PageEntity page = getPage(pageEntity,count,partnerList);
        return page;
    }

    @Override
    public PageEntity queryMyPartner(Integer empId, Integer pageNumber, Integer pageSize) {
        int count = sysUnderlingDao.countMyPartner(empId);
        if (null == pageSize || pageSize >= CommonConstants.maxPageSize) {
            pageSize = CommonConstants.pageSize;
        }
        if (null == pageNumber || pageNumber.intValue() == NumberConstants.NUM_0) {
            pageNumber = CommonConstants.pageNumber;
        }else {
            pageNumber = (pageNumber-1) * pageSize;
        }
        List<PartnerResponseDto> partnerList = baseDao.queryMyPartnerList(empId,pageNumber,pageSize);
        List<Integer> myPartnerList = sysUnderlingDao.queryUnderlingList(empId);
        buildPartner(partnerList,myPartnerList);
        PageEntity page = getPage(pageNumber,pageSize,count,partnerList);
        return page;
    }

    /**
     * 补全合伙人属性
     * @param partnerList
     */
    private void buildPartner(List<PartnerResponseDto> partnerList,List<Integer> myPartnerList) {
        for (PartnerResponseDto partnerResponseDto : partnerList) {
            if (null != myPartnerList && myPartnerList.contains(partnerResponseDto.getPartId())) {
                partnerResponseDto.setIsCollect(NumberConstants.NUM_1);
            }else {
                partnerResponseDto.setIsCollect(NumberConstants.NUM_0);
            }
            if (StringUtils.isNotBlank(partnerResponseDto.getHeadImageUrl())) {
                partnerResponseDto.setHeadImageUrl(CommonConstants.IMG_URL + partnerResponseDto.getHeadImageUrl());
            }
            partnerResponseDto.setUserTypeName(UserTypeEnum.getNameByCode(partnerResponseDto.getUserType()));
            String provinceName = StringUtils.isBlank(partnerResponseDto.getProvince()) ? "" :
                    partnerResponseDto.getProvince();
            String cityName = StringUtils.isBlank(partnerResponseDto.getCity()) ? "" : partnerResponseDto.getCity();
            String areaName = StringUtils.isBlank(partnerResponseDto.getArea()) ? "" : partnerResponseDto.getArea();
            partnerResponseDto.setAgencyZone(provinceName.concat(cityName).concat(areaName));
        }

    }
}
